package cn.net.shuxing.yifu.common.back.controller;

import cn.net.shuxing.yifu.common.bean.UserBankCard;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.service.DeclarationFormService;
import cn.net.shuxing.yifu.common.service.UserBankCardService;
import cn.net.shuxing.yifu.common.service.UserWithdrawalCashService;
import cn.net.shuxing.yifu.mysiteforme.admin.base.MySysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 股东报单管理
 */
@RestController
@RequestMapping("/back/declaration")
public class DeclarationFormController {


    @Autowired
    DeclarationFormService declarationFormService;

    @Autowired
    UserWithdrawalCashService userWithdrawalCashService;

    @Autowired
    UserBankCardService userBankCardService;

    /**
     * 查询股东报单列表
     * @return
     */
    @RequestMapping("/list")
    public BaseDto selectList(@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum,
                              @RequestParam(defaultValue = "10",value = "pageSize")Integer pageSize,
                              Long userId,Integer dealStatus){
        if(userId == null) {
            userId = MySysUser.id();
        }
        Integer startPageNo = null;
        if (null != pageSize && null != pageNum) {
            startPageNo = (pageNum - 1) * pageSize;
        }
        BaseDto baseDto = new BaseDto();
        baseDto.setData(declarationFormService.selectList(startPageNo,pageSize,userId,dealStatus));
        return baseDto;
    }

    /**
     * 新增报单
     * @param nickName
     * @param tel
     * @param coinNum
     * @param unitPrice
     * @param limitDay
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseDto add(String nickName, String tel, BigDecimal coinNum, BigDecimal unitPrice, BigDecimal publicPrice,Integer limitDay){
        return declarationFormService.add(nickName,tel,coinNum,unitPrice,publicPrice,limitDay);
    }

    /**
     * 处理报单——充值
     */
    @RequestMapping(value = "/form/deposit",method = RequestMethod.POST)
    public BaseDto formDeposit(String password,Long id){
        return declarationFormService.formDeposit(password,id);
    }

    /**
     * 处理报单——拒绝
     * @return
     */
    @RequestMapping("/form/refuse")
    public BaseDto formRefuse(Long id){
        return declarationFormService.formRefuse(id);
    }

    /**
     * 财务管理--佣金记录列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/comm/list")
    public BaseDto commList(@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum,
                            @RequestParam(defaultValue = "10",value = "pageSize")Integer pageSize){
        Long userId = MySysUser.id();
        Integer startPageOn = null;
        if (null != pageSize && null != pageNum) {
            startPageOn = (pageNum - 1) * pageSize;
        }
        BaseDto baseDto = new BaseDto();
        baseDto.setData(declarationFormService.commList(startPageOn,pageSize,userId));
        return baseDto;
    }

    /**
     * 财务管理--提现记录
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/withdrawal/list")
    public BaseDto withdrawalList(@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum,
                                  @RequestParam(defaultValue = "10",value = "pageSize")Integer pageSize){
        Long userId = MySysUser.id();
        Integer startPageNo = null;
        if (null != pageSize && null != pageNum) {
            startPageNo = (pageNum - 1) * pageSize;
        }
        BaseDto baseDto = new BaseDto();
        baseDto.setData(userWithdrawalCashService.getWithdrawalList(startPageNo,pageSize,Integer.valueOf(userId.toString()),2));
        return baseDto;
    }

    /**
     * 报单详情
     * @return
     */
    @RequestMapping("/detail")
    public BaseDto detail(Long id){
        return declarationFormService.detail(id);
    }


    /**
     * 兼职股东-结算佣金
     * @return
     */
    @RequestMapping(value = "/balance/comm",method = RequestMethod.POST)
    public BaseDto balanceComm(Long id){
        return declarationFormService.balanceComm(id);
    }


    /**
     * 兼职股东-新增银行卡信息
     */
    @RequestMapping(value = "/bank/card/add")
    public BaseDto addBankCard(String openBank,String holderName,String bankCardNum){
        BaseDto baseDto = new BaseDto();
        Long userId = MySysUser.id();
        UserBankCard userBankCard = userBankCardService.selectByUserIdAndType(Integer.valueOf(userId.toString()),2);
        if(userBankCard == null){
            //新增银行卡信息
            UserBankCard userBankCard1 = new UserBankCard();
            userBankCard1.setType(2);
            userBankCard1.setUserId(Integer.valueOf(userId.toString()));
            userBankCard1.setOpenBank(openBank);
            userBankCard1.setHolderName(holderName);
            userBankCard1.setBankCardNum(bankCardNum);
            Integer m = userBankCardService.insert(userBankCard);
            if(m > 0){
                baseDto.setData("添加银行卡成功");
            }else{
                baseDto.setData("添加银行卡失败");
            }
        }else{
            //更新银行卡信息
            userBankCard.setOpenBank(openBank);
            userBankCard.setHolderName(holderName);
            userBankCard.setBankCardNum(bankCardNum);
            userBankCardService.update(userBankCard);
            baseDto.setData("操作成功");
        }
        return baseDto;
    }







}
